package com.exception;

import com.enums.ServiceExceptionEnum;
import com.util.CommonFunctionUtil;
import lombok.Getter;

import java.io.Serial;

@Getter
public class ServiceException extends RuntimeException{
    @Serial
    private static final long serialVersionUID = 5314839945354182507L;

    private final String code;
    private final String msg;

    public ServiceException(String code){
        this.code = code;
        ServiceExceptionEnum errorEnum= CommonFunctionUtil.getServiceError(code);
        if(errorEnum != null){
            this.msg = errorEnum.getMsg();
        }else{
            this.msg = "";
        }
    }

    public ServiceException(String code, String... params){
        this.code = code;
        ServiceExceptionEnum errorEnum= CommonFunctionUtil.getServiceError(code);
        if(errorEnum != null){
            if(params == null || params.length == 0){
                this.msg = errorEnum.getMsg();
            }else{
                this.msg = errorEnum.getFormattedMsg(params);
            }
        }else{
            this.msg = params[0];
        }
    }

    public ServiceException(String code, Integer... params){
        this.code = code;
        ServiceExceptionEnum errorEnum = CommonFunctionUtil.getServiceError(code);
        if(errorEnum != null){
            if(params == null || params.length == 0){
                this.msg = errorEnum.getMsg();
            }else{
                this.msg = errorEnum.getFormattedMsg(params);
            }
        }else{
            this.msg = String.valueOf(params[0]);
        }
    }
}
